package com.xyy.saas.payment.util;

import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.cores.enums.BusinessIdTypeEnum;
import com.xyy.saas.payment.cores.enums.InvokerEnum;
import com.xyy.saas.payment.cores.vo.AccountVo;
import util.LukeRsaUtil;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wuqiongda
 * @Date: 2019/11/19 11:17
 */
public class SignUtil {

    public static boolean doCheck(Map<String, Object> content, String sign, String publicKey, String encode){
        //判断是否开启验签模式
        if (!Boolean.parseBoolean(ApolloUtil.getProperty(Constants.IS_SIGN))){
            return true;
        }
        return LukeRsaUtil.doCheck(content, sign, publicKey, encode);
    }

    public static boolean doCheck(Map<String, Object> content, String sign, String publicKey){
        //判断是否开启验签模式
        if (!Boolean.parseBoolean(ApolloUtil.getProperty(Constants.IS_SIGN))){
            return true;
        }
        return LukeRsaUtil.doCheck(content, sign, publicKey);
    }

    public static boolean doCheck(Object o, String sign, String publicKey) throws IllegalAccessException {
        //判断是否开启验签模式
        if(!Boolean.parseBoolean(ApolloUtil.getProperty(Constants.IS_SIGN))){
            return true;
        }

        return LukeRsaUtil.doCheck(o, sign, publicKey);
    }

    public static String mD5Encode(String msg) {
        try {
            // 加密对象，指定加密方式
            MessageDigest md5 = MessageDigest.getInstance("md5");
            // 准备要加密的数据
            byte[] b = msg.getBytes();
            // 加密
            byte[] digest = md5.digest(b);
            // 十六进制的字符
            char[] chars = new char[] { '0', '1', '2', '3', '4', '5',
                    '6', '7' , '8', '9', 'A', 'B', 'C', 'D', 'E','F' };
            StringBuffer sb = new StringBuffer();
            // 处理成十六进制的字符串(通常)
            for (byte bb : digest) {
                sb.append(chars[(bb >> 4) & 15]);
                sb.append(chars[bb & 15]);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws IllegalAccessException {
        String pri = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ5+o9ziVI8QLMaD3j3B6hVnebeEYEiP3TqkbeikDkrBxifTFQEFXT+nMvSBePIMxhtWK7+owuF9A95o7VQzAHd1xJxKtfrCRU5NDIsW7voVBahTsmzRNrk0/uHW4p3dcar9eRMDpn1BdL8qwQhtVGXJVpN2c5OlUHgGvVdX6vQ5AgMBAAECgYBVPckFcBiIROu/7IYnctZhoqA8yg46mF0kP/D4sNsa67KWEpl2ThtDs7nc9JFGpQYbB7JJxwrg2SqugII7yZ5SyKteNdlaPuNkyoEE7CQw2EmtY4Rk1yFuCusEqaBIM5XufCND7+gG7YC9ViRBJ7oqYJnMkCWvPNO4vHrCGhVqnQJBAMxnaXEutSS0mvDAmMgaEPkENg1r+BHCB5Fp2UC0DUC296QqmIEr/fC2zLbdK0UPegxJLQNQW4NS+IcaXJyMMSMCQQDGgJIJk5bfXReEuVxntOyfCL+CFn0H+4Qpoq0y0vUiLeJriAQG0YRSsroT8s1CN7r9oGyssCI+3G7BfoPJU3DzAkEApKdI0NuHH6I8jWMXr1kQAwfZYsZudh+OaRyS3QQtCMxaf+uwHZ2KWbVnAob2rBLjhbIMiHbawNa9TFd38sMbcQJAMfz+ydqZC/btjUcAtj0huYsCb0gqhHAagEGvTDVD9i+H7Xx7RxxMLmdD5PIoLpzKQkS8GWDhvuLUIXYyyehxcQJAJQrnHTftF3YLsv651gDQ2EyOaQiBsjTcqN/bP8s1SrWD8XKYYounQ5YFLjnVtovDpZmeBNwokaTpBlFfEccq/w==";
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCefqPc4lSPECzGg949weoVZ3m3hGBIj906pG3opA5KwcYn0xUBBV0/pzL0gXjyDMYbViu/qMLhfQPeaO1UMwB3dcScSrX6wkVOTQyLFu76FQWoU7Js0Ta5NP7h1uKd3XGq/XkTA6Z9QXS/KsEIbVRlyVaTdnOTpVB4Br1XV+r0OQIDAQAB";
        AccountVo accountVo = new AccountVo();
        accountVo.setBusinessId("DRUGSTORE-14775");
        accountVo.setBusinessIdType(BusinessIdTypeEnum.YIKUAIQIAN_DOC);
        accountVo.setInvoker(InvokerEnum.YIKUAIQIAN_DOC);
        Map<String ,Object> map = new HashMap<>(8);
        map.put("businessId", "DRUGSTORE-14775");
        map.put("invoker", "YIKUAIQIAN_DOC");
        map.put("businessIdType", "YIKUAIQIAN_DOC");
        System.out.println(LukeRsaUtil.sign(map, pri));
        boolean b = LukeRsaUtil.doCheck(accountVo, "geVDw10MH659ds/Co4NKXFMqlfz7epHncDW+G/P35zeEMS/43QD8iNw+PM4hvtQyqQRFYV3encH/klUxJ7yo/EwXbkCYR4T2E8pJQ1JoaSVVkC98Pux/WZUyziUI2VjTt15yREBTGHLwRcnSHifVQeIgIv2jDUb19hNrD+QEoSM=", publicKey);
        System.out.println(b);
        System.out.println(LukeRsaUtil.sign(accountVo, pri));
    }
}
